package com.it.controller;

import com.alibaba.fastjson.JSONObject;
import com.it.common.Result;
import com.it.dao.TagMapMapper;
import com.it.domain.Song;
import com.it.domain.SongList;
import com.it.domain.SongSinger;
import com.it.domain.User;
import com.it.service.LoginService;
import com.it.service.SongListService;
import com.it.common.CONSTS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/SongList")
public class SongListController {

    @Autowired
    private SongListService songListService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private TagMapMapper tagMapMapper;

    @PostMapping("/AddSongList")
    public Object AddSongList(@RequestBody SongList songlist, HttpServletRequest request){

        String token = request.getHeader("token");
        if(token == null || token == ""){
            return Result.fail("404","登录过期");
        }
        User user = loginService.CheckToken(token);
        if(user == null){
            return Result.fail("404","登录过期");
        }

        String title = songlist.getTitle().trim();
        String introduce = songlist.getIntroduce().trim();
        String pic = songlist.getPic().trim();
        Date createTime = songlist.getCreateTime();

        SongList songList = new SongList();
        songList.setCreateTime(createTime);
        songList.setPic(pic);
        songList.setIntroduce(introduce);
        songList.setTitle(title);
        songList.setListUserid(user.getId());
        System.out.println(songList.getListUserid());
       return songListService.AddSongList(songList);
    }

    @PostMapping("/SelectAll")
    public Map<String,Object> SelectAll(@RequestParam("PageNum") Integer PageNum, @RequestParam("PageSize") Integer PageSize,@RequestParam("title") String title){
        System.out.println(PageNum);
        System.out.println(PageSize);
        PageNum = (PageNum-1)*PageSize;
        title = '%' + title + '%';
        Map<String,Object> res = new HashMap<String,Object>();

        Integer total = songListService.SelectTotal();
        List<SongList> data = songListService.SelectAll(PageNum,PageSize,title);

        res.put("total",total);
        res.put("data",data);
        System.out.println(res);
        return res;
    }

    @DeleteMapping("/DeleteSongList/{id}")
    public Object DeleteSongList(@PathVariable Integer id){
        JSONObject jsonObject = new JSONObject();
        boolean flag = songListService.DeleteSongList(id);
        if (flag) {
            jsonObject.put(CONSTS.CODE, 200);
            jsonObject.put(CONSTS.MSG, "删除成功");
        } else {
            jsonObject.put(CONSTS.CODE, 500);
            jsonObject.put(CONSTS.MSG, "删除失败");
        }
        return jsonObject;
    }

    @PutMapping("/UpdateSongList")
    public Object UpdateSongList(@RequestBody SongList songlist){

        tagMapMapper.DeleteTagMap(songlist.getId());
        JSONObject jsonobject = new JSONObject();
        String title = songlist.getTitle().trim();
        String introduce = songlist.getIntroduce().trim();
        String pic = songlist.getPic().trim();
        Date createTime = songlist.getCreateTime();

        SongList songList = new SongList();
        songList.setCreateTime(createTime);
        songList.setPic(pic);
        songList.setIntroduce(introduce);
        songList.setTitle(title);
        songList.setId(songlist.getId());
        boolean flag = songListService.UpdateSongList(songList);
        if (flag) {
            jsonobject.put(CONSTS.CODE, 200);
            jsonobject.put(CONSTS.MSG, "更新成功");
        } else {
            jsonobject.put(CONSTS.CODE, 400);
            jsonobject.put(CONSTS.MSG, "更新失败");
        }
        return jsonobject;
    }

    @GetMapping("/SelectById/{id}")
    public SongList SelectById(@PathVariable Integer id){
        return songListService.SelectById(id);
    }


    @GetMapping("/GetAll")
    public List<SongList> GetAll(){
        return songListService.GetAll();
    }

    @PostMapping("/SelectSongList")
    public SongList SelectSongList(@RequestParam String title,HttpServletRequest request){

        String token = request.getHeader("token");
        User user = loginService.CheckToken(token);
        return songListService.SelectSongList(title, user.getId());
    }

    @PostMapping("/SelectMainSongList")
    public List<SongList> SelectMainSongList(@RequestParam Integer[] ids){
        if(ids == null){
            return null;
        }
        else{
            List<SongList> songLists = songListService.SelectMainSongList(ids);
            System.out.println(songLists);
            return songLists;
        }
    }

    @GetMapping("/getSongListSong/{SongListId}")
    public List<SongSinger> getSongListSong(@PathVariable Integer SongListId){

        List<SongSinger> songSingers = new ArrayList<>();
        List<SongList> songLists = songListService.getSongListSong(SongListId);
        for(SongList s : songLists){
            SongSinger songSinger = new SongSinger();
            for(Song s1 : s.getSongLists()){
                songSinger.setSongId(s1.getId());
                songSinger.setUrl(s1.getUrl());
                songSinger.setName(s1.getName());
                songSinger.setImage(s1.getImage());
                songSinger.setUrl(s1.getUrl());
                songSinger.setLyric(s1.getLyric());
                songSinger.setAlbum(s1.getAlbum());
                songSinger.setSinger(s1.getSinger().getName());
            }
            songSingers.add(songSinger);
        }
        return songSingers;
    }

    @GetMapping("/SelectTotalSongList")
    public Integer SelectTotalSongList(){
        return songListService.SelectTotal();
    }
}
